/**
 * 定义ui公共模块
 */
(function(window, angular, undefined){

	var module = angular.module('water.common.dateUtil',[]);
	
	module.service('$dateUtil',function($waterDataCommon){
		
		/**
		 * 构造日期函数
		 * 接受 2017/01/05 16:10:58格式的时间，
		 * 如果是2017-01-05 16:11:18 格式的，自动替换成上面的格式
		 * WWTP_ID
		 * STATION_ID
		 */
		this.createDate = function(dateStr){
			
			if(!dateStr){
				return;
			}
			
			dateStr = dateStr.replace(/-/g,"/");
			var date = new Date(dateStr);
			return date;
		}
		
		/**
		 * 对秒进行加减操作
		 */
		this.addSecond = function(date,seconds){
			var time = date.valueOf();
			time = time + seconds * 1000;
			var result = new Date(time);
			return result;
		}
		
		/**
		 * 对分钟进行加减操作
		 */
		this.addMinute = function(date,minutes){
			var time = date.valueOf();
			time = time + minutes * 1000 * 60;
			var result = new Date(time);
			return result;
		}
		
		/**
		 * 对小时进行加减操作
		 */
		this.addHour = function(date,hours){
			var time = date.valueOf();
			time = time + hours * 1000 * 60 * 60;
			var result = new Date(time);
			return result;
		}
		
		/**
		 * 对天进行加减操作
		 */
		this.addDay = function(date,days){
			var time = date.valueOf();
			time = time + days * 1000 * 60 * 60 * 24;
			var result = new Date(time);
			return result;
		}
		
		/**
		 * 对月进行加减操作
		 */
		this.addMonth = function(date,months){
			var time = date.valueOf();
			var result = new Date(time);
			result.setMonth(result.getMonth() + months);
			return result;
		}
		
		/**
		 * 对Date的扩展，将 Date 转化为指定格式的String
		 * 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
		 * 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
		 * 例子： 
		 * (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
		 * (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
		 */
		this.formatDate = function(date,fmt){
			if(!fmt){
				fmt = "yyyy-MM-dd hh:mm:ss";
			}
			var dateInfo = {
			        "M+": date.getMonth() + 1, //月份 
			        "d+": date.getDate(), //日 
			        "h+": date.getHours(), //小时 
			        "m+": date.getMinutes(), //分 
			        "s+": date.getSeconds(), //秒 
			        "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
			        "S": date.getMilliseconds() //毫秒 
			    };
			 if (/(y+)/.test(fmt)) {
				 fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
			 }
			 for (var k in dateInfo){
				 if (new RegExp("(" + k + ")").test(fmt)) {
					 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (dateInfo[k]) : (("00" + dateInfo[k]).substr(("" + dateInfo[k]).length)));
				 }
			 }
			 return fmt;
		}
		/**
		 * 获取一年里面的所有周
		 */
		this.getYearWeek = function(year) {
			var weeks = [];
			var weeksum = 0;
			for(var month=1;month<=12;month++){
				var today = new Date();//今天
				
				var t1 = 0;//
				var t2 = 0;
				
		    	var currentDate = new Date();
		    	//周一为每星期第一天
		    	currentDate.setFullYear(year, month-1, 1);
		    	var w1 = currentDate.getDay();
		    	if (w1 == 0) {
		    	  w1 = 7;
		    	}
		    	
		    	//每月天数
		    	currentDate.setFullYear(year, month, 0);
		    	var dd = currentDate.getDate();
		    	// first Monday 第一个周一
		    	if (w1 != 1) {
		    		d1 = 7 - w1 + 2;
		    		if(month == 1){
		    			weeksum++; //如果一月一号不是星期一，则一月一号算是第一周的范围
		    		}
		    	} else {
		    		d1 = 1;
		    	}
		    	
		    	//本月有多少周
		    	week_count = Math.ceil((dd-d1+1)/7);
		    	
		    	for (var i = 0; i < week_count; i++) {
		    		weeksum++;
		    		var monday = d1+i*7;
		    		var sunday = monday + 6;
		    		var from = month+"月"+monday +"日";
		    		var start = "";
		    		if(month<10){
		    			start = year+"-0"+month+"-"+monday;
		    		}
		    		else{
		    			 start = year+"-"+month+"-"+monday;
		    		}
		    		
		    		t1 = currentDate.setFullYear(year,month-1,monday);
		    		
		    		var to;
		    		var end;
		    		if (sunday <= dd) {
		    			to = month+"月"+sunday+"日";
		    			if(month<10){
		    				end=year+"-0"+month+"-"+sunday;
			    		}
			    		else{
			    			end=year+"-"+month+"-"+sunday;
			    		}
		    			t2 = currentDate.setFullYear(year,month-1,sunday);
		    		} else {
		    			currentDate.setFullYear(year, month-1, sunday);
		    			to = (currentDate.getMonth()+1)+"月"+currentDate.getDate()+"日";
		    			if((currentDate.getMonth()+1)<10){
		    				end=currentDate.getFullYear()+"-0"+(currentDate.getMonth()+1)+"-"+currentDate.getDate();
		    			}
		    			else{
		    				end=currentDate.getFullYear()+"-"+(currentDate.getMonth()+1)+"-"+currentDate.getDate();
		    			}
		    			t2 = currentDate.setFullYear(year, month-1, sunday+1);
		    		}
		    		
		    		var obj=new Object();
		    		obj.name="第"+(weeksum)+"周 " + from + " 到 " + to;
		    		obj.value=start+","+end+","+weeksum;
		    		obj.today = false;
		    		if(today.getTime()>=t1 && today.getTime()<t2){
		    			obj.today = true;
		    		}
		    		weeks.push(obj);
		    	}
			}
			return weeks;
	    };
		
	});
	
}(window, angular));